package com.ler.yrmall.member.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.dto.LoginUser;
import com.ler.yrmall.enums.BasicStatusEnum;
import com.ler.yrmall.exception.BusinessException;
import com.ler.yrmall.member.entity.MemberReceiveAddressEntity;
import com.ler.yrmall.member.mapper.MemberReceiveAddressMapper;
import com.ler.yrmall.member.service.MemberReceiveAddressService;
import com.ler.yrmall.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author leron
 */
@Service
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddressEntity> implements MemberReceiveAddressService {


    @Override
    public List<MemberReceiveAddressEntity> memberAddressList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return this.list(Wrappers.<MemberReceiveAddressEntity>lambdaQuery().eq(MemberReceiveAddressEntity::getMemberId, loginUser.getId()));
    }

    @Override
    public MemberReceiveAddressEntity defaultAddress() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return this.getOne(Wrappers.<MemberReceiveAddressEntity>lambdaQuery()
                .eq(MemberReceiveAddressEntity::getMemberId,loginUser.getId())
                .eq(MemberReceiveAddressEntity::getIsDefault, BasicStatusEnum.SystemEnableStatus.ENABLED.getCode()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAddress(MemberReceiveAddressEntity memberReceiveAddress) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        memberReceiveAddress.setMemberId(loginUser.getId());
        List<MemberReceiveAddressEntity> mr = this.memberAddressList();
        if (mr.isEmpty()) {
            memberReceiveAddress.setIsDefault(true);
            this.save(memberReceiveAddress);
            return;
        }
        if (memberReceiveAddress.isDefault()) {
            this.update(Wrappers.<MemberReceiveAddressEntity>lambdaUpdate()
                    .eq(MemberReceiveAddressEntity::getMemberId,loginUser.getId())
                    .set(MemberReceiveAddressEntity::getIsDefault,0));
        }
        this.save(memberReceiveAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(MemberReceiveAddressEntity memberReceiveAddress) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        memberReceiveAddress.setMemberId(loginUser.getId());
        if(memberReceiveAddress.isDefault()) {
            this.update(Wrappers.<MemberReceiveAddressEntity>lambdaUpdate()
                    .eq(MemberReceiveAddressEntity::getMemberId,loginUser.getId())
                    .set(MemberReceiveAddressEntity::getIsDefault,0));
        }
        this.updateById(memberReceiveAddress);
    }

    @Override
    public void deleteAddress(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MemberReceiveAddressEntity ma = this.getById(id);
        if (ma.getIsDefault()) {
            throw new BusinessException("默认地址不允许删除");
        }
        this.remove(Wrappers.<MemberReceiveAddressEntity>lambdaQuery()
                .eq(MemberReceiveAddressEntity::getMemberId,loginUser.getId())
                .eq(MemberReceiveAddressEntity::getId,id));
    }
}